Passed
Push — master ( 1c5edc...aba8d4 )
by Fran
06:33 queued 03:02
created

index.js ➔ prepareProperties   C

Complexity

Conditions 10

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 15
rs 5.9999
c 0
b 0
f 0
cc 10

How to fix   Complexity   

Complexity

Complex classes like index.js ➔ prepareProperties often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
app.controller('IndexCtrl', ['$scope', '$msgSrv', '$timeout', '$log',
2
    ($scope, $msgSrv, $timeout, $log) => {
3
        $scope.show = false;
4
        $scope.indexProperties = [];
5
6
        $scope.getIndexLegend = () => {
7
            let label = '-';
0 ignored issues
show
Unused Code introduced by
The assignment to variable label seems to be never used. Consider removing it.
Loading history...
8
            try {
9
                label = $scope.model['name'];
10
            } catch(err) {
11
                $log.warn(err.message);
12
            }
13
14
            return label;
15
        };
16
17
        $scope.toggleIndexForm = () => {
18
            $scope.show = !$scope.show;
19
            if($scope.show) {
20
                $msgSrv.send('index.edit', $scope.model.id);
21
            }
22
        };
23
24
        $scope.removeIndex = () => {
25
            $msgSrv.send('index.remove', $scope.index);
26
        };
27
28
        $scope.$on('index.edit', (ev, id) => {
29
            if($scope.model.id !== id) {
30
                $scope.show = false;
31
            }
32
        });
33
34
        function prepareProperties() {
0 ignored issues
show
Bug introduced by
The function prepareProperties is declared conditionally. This is not supported by all runtimes. Consider moving it to root scope or using var prepareProperties = function() { /* ... */ }; instead.
Loading history...
35
            for(let i in $scope.properties) {
36
                let property = $scope.properties[i];
37
                $scope.indexProperties.push({
38
                    id: property.name + '.ASC',
39
                    name: property.name,
40
                    mode: 'ASC'
41
                });
42
                $scope.indexProperties.push({
43
                    id: property.name + '.DESC',
44
                    name: property.name,
45
                    mode: 'DESC'
46
                });
47
            }
48
        }
49
50
        prepareProperties();
51
    }
52
]);
53
app.directive('indexes', [() => {
54
    return {
55
        restrict: 'E',
56
        replace: true,
57
        controller: 'IndexCtrl',
58
        templateUrl: '/js/composer/index.html',
59
        scope: {
60
            properties: '=',
61
            model: '=',
62
            index: '='
63
        }
64
    };
65
}]);
66